#include "options.h"
#include "ui_optionsdialog.h"
#include "crypt.h"

Options::Options(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::OptionsDialog)
{
    timeout = 20;
    m_ui->setupUi(this);
    optionsWindowActive = false;
}

Options::~Options()
{
    delete m_ui;
}

void Options::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Options::save()
{
    QSettings set("config.ini", QSettings::IniFormat);
    set.setValue("email", email);
    set.setValue("crypted", isCrypted);
    QString tmp;
    if (isCrypted)
        crypt(password, tmp);
    else
        tmp = password;
    set.setValue("password", tmp);
    set.setValue("timeout", timeout);
    set.setValue("autostart", autostart);
    set.setValue("use_proxy", useProxy);
    set.setValue("proxy_host", proxyHost);
    set.setValue("proxy_port", proxyPort);
    set.setValue("use_proxy_authorization", useAuthorization);
    set.setValue("proxy_username", proxyUserName);
    set.setValue("proxy_password", proxyPassword);
}

void Options::load()
{
    QSettings set("config.ini", QSettings::IniFormat);
    QVariant q = set.value("email", "");
    email = q.toString();
    q = set.value("crypted", true);
    isCrypted = q.toBool();
    q = set.value("password", "");
    if (isCrypted)
        crypt(q.toString(), password);
    else
        password = q.toString();
    q = set.value("timeout", 20);
    timeout = q.toInt();
    q = set.value("autostart", true);
    autostart = q.toBool();
    q = set.value("use_proxy", false);
    useProxy = q.toBool();
    q = set.value("proxy_host", "");
    proxyHost = q.toString();
    q = set.value("proxy_port", 3128);
    proxyPort = q.toInt();
    q = set.value("use_proxy_authorization", false);
    useAuthorization = q.toBool();
    q = set.value("proxy_username", "");
    proxyUserName = q.toString();
    q = set.value("proxy_password", "");
    if (isCrypted)
        crypt(q.toString(), proxyPassword);
    else
        proxyPassword = q.toString();
}

void Options::showOptions(bool *mRunning, bool *mStopSleep)
{
    int availableWidth = QApplication::desktop()->availableGeometry().width();
    int availableHeight = QApplication::desktop()->availableGeometry().height();
    int availableLeft = QApplication::desktop()->availableGeometry().left();
    int availableTop = QApplication::desktop()->availableGeometry().top();
    this->setGeometry(availableLeft + (availableWidth - this->size().width()) / 2, availableTop + (availableHeight - this->size().height()) / 2, this->size().width(), this->size().height());
    optionsWindowActive = true;
    running = mRunning;
    stopSleep = mStopSleep;
    load();
    m_ui->emailEdit->setText(email);
    m_ui->passEdit->setText(password);
    m_ui->cryptedBox->setChecked(isCrypted);
    m_ui->timeoutSpin->setValue(timeout);
    m_ui->autostartBox->setChecked(autostart);
    m_ui->useProxyBox->setChecked(useProxy);
    m_ui->proxyHostEdit->setText(proxyHost);
    m_ui->proxyPortSpin->setValue(proxyPort);
    m_ui->useAuthorizationBox->setChecked(useAuthorization);
    m_ui->proxyUserNameEdit->setText(proxyUserName);
    m_ui->proxyPasswordEdit->setText(proxyPassword);
    if (useProxy)
    {
        m_ui->proxyHostEdit->setEnabled(true);
        m_ui->proxyHostLabel->setEnabled(true);
        m_ui->proxyPortSpin->setEnabled(true);
        m_ui->proxyPortLabel->setEnabled(true);
        m_ui->useAuthorizationBox->setEnabled(true);
        if (useAuthorization)
        {
            m_ui->proxyUserNameEdit->setEnabled(true);
            m_ui->proxyUserNameLabel->setEnabled(true);
            m_ui->proxyPasswordEdit->setEnabled(true);
            m_ui->proxyPasswordLabel->setEnabled(true);
        }
        else
        {
            m_ui->proxyUserNameEdit->setEnabled(false);
            m_ui->proxyUserNameLabel->setEnabled(false);
            m_ui->proxyPasswordEdit->setEnabled(false);
            m_ui->proxyPasswordLabel->setEnabled(false);
        }
    }
    else
    {
        m_ui->proxyHostEdit->setEnabled(false);
        m_ui->proxyHostLabel->setEnabled(false);
        m_ui->proxyPortSpin->setEnabled(false);
        m_ui->proxyPortLabel->setEnabled(false);
        m_ui->useAuthorizationBox->setEnabled(false);
        m_ui->proxyUserNameEdit->setEnabled(false);
        m_ui->proxyUserNameLabel->setEnabled(false);
        m_ui->proxyPasswordEdit->setEnabled(false);
        m_ui->proxyPasswordLabel->setEnabled(false);
    }
    this->show();
}

void Options::hideOptions()
{
    optionsWindowActive = false;
    this->hide();
}

void Options::on_useProxyBox_stateChanged(int )
{
    if (m_ui->useProxyBox->checkState())
    {
        m_ui->proxyHostEdit->setEnabled(true);
        m_ui->proxyHostLabel->setEnabled(true);
        m_ui->proxyPortSpin->setEnabled(true);
        m_ui->proxyPortLabel->setEnabled(true);
        m_ui->useAuthorizationBox->setEnabled(true);
        if (m_ui->useAuthorizationBox->checkState())
        {
            m_ui->proxyUserNameEdit->setEnabled(true);
            m_ui->proxyUserNameLabel->setEnabled(true);
            m_ui->proxyPasswordEdit->setEnabled(true);
            m_ui->proxyPasswordLabel->setEnabled(true);
        }
        else
        {
            m_ui->proxyUserNameEdit->setEnabled(false);
            m_ui->proxyUserNameLabel->setEnabled(false);
            m_ui->proxyPasswordEdit->setEnabled(false);
            m_ui->proxyPasswordLabel->setEnabled(false);
        }
    }
    else
    {
        m_ui->proxyHostEdit->setEnabled(false);
        m_ui->proxyHostLabel->setEnabled(false);
        m_ui->proxyPortSpin->setEnabled(false);
        m_ui->proxyPortLabel->setEnabled(false);
        m_ui->useAuthorizationBox->setEnabled(false);
        m_ui->proxyUserNameEdit->setEnabled(false);
        m_ui->proxyUserNameLabel->setEnabled(false);
        m_ui->proxyPasswordEdit->setEnabled(false);
        m_ui->proxyPasswordLabel->setEnabled(false);
    }
}

void Options::on_useAuthorizationBox_stateChanged(int )
{
    if (m_ui->useAuthorizationBox->checkState())
    {
        m_ui->proxyUserNameEdit->setEnabled(true);
        m_ui->proxyUserNameLabel->setEnabled(true);
        m_ui->proxyPasswordEdit->setEnabled(true);
        m_ui->proxyPasswordLabel->setEnabled(true);
    }
    else
    {
        m_ui->proxyUserNameEdit->setEnabled(false);
        m_ui->proxyUserNameLabel->setEnabled(false);
        m_ui->proxyPasswordEdit->setEnabled(false);
        m_ui->proxyPasswordLabel->setEnabled(false);
    }
}

void Options::on_acceptButton_clicked()
{
    email = this->m_ui->emailEdit->text();
    isCrypted = this->m_ui->cryptedBox->checkState();
    password = this->m_ui->passEdit->text();
    timeout = this->m_ui->timeoutSpin->value();
    autostart = this->m_ui->autostartBox->checkState();
    useProxy = this->m_ui->useProxyBox->checkState();
    proxyHost = this->m_ui->proxyHostEdit->text();
    proxyPort = this->m_ui->proxyPortSpin->value();
    useAuthorization = this->m_ui->useAuthorizationBox->checkState();
    proxyUserName = this->m_ui->proxyUserNameEdit->text();
    proxyPassword = this->m_ui->proxyPasswordEdit->text();
    save();
    *running = false;
    *stopSleep = true;
    hideOptions();
}

void Options::on_cancelButton_clicked()
{
    hideOptions();
}

void Options::closeEvent(QCloseEvent *event)
{
    hideOptions();
    event->ignore();
}
